<?php
class Route_Router extends Controller {
	function Route_Router()
	{
		parent::Controller();
	}
	
	function index()
	{
		// this action is responsible for taking a root request to and dispatching it to the appropriate controller/action
		// we handle the following:
		// 1. kylebragger.com 	-- custom domain (specific bricabox) 	-> bricabox
		// 2. sub.bricabox.com 	-- subdomain (specific bricabox) 		-> bricabox
		// 3. bricabox.com 		-- top level, global site 				-> homepage
		
		global $BBX;
		
		if (isset($BBX['bricabox']))
		{
			// bricabox/show
			$this->load->model('Bricabox_model');
			$this->load->model('Block_model');
			$this->load->model('Content_model');
			$this->load->model('Membership_model');
			$this->load->model('Activity_model');
			
			// check privacy
			if (!$BBX['bricabox']->privacy)
			{
				// make sure someone is logged in and a member
				if (!$this->Membership_model->is_member($BBX['bricabox']->id))
				{
					header("Location: /private");
					exit();
				}
			}
			
			$avail_blocks = $this->db->query('select * from blocks where lives_on = ? or lives_on = ? order by name asc', array('homepage', 'both'));
			
			$data = array(
				'BBX'			=> $BBX,
				'bricabox' 		=> $BBX['bricabox'],
				'page_title'	=> $BBX['bricabox']->name,
				'blocks'		=> $this->Block_model->load_blocks_for_location('homepage'),
				'content'		=> $this->Content_model->load_all($BBX['bricabox']->id),
				'avail_blocks'	=> $avail_blocks,
				'activity'		=> $this->Activity_model->get_activity(array('bricabox_id' => $BBX['bricabox']->id))
			);
			if (!$this->userland->logged_in() || (!$this->Membership_model->is_owner($BBX['bricabox']->id, $this->userland->current_user()->id)))
				$this->Bricabox_model->record_pageview($BBX['bricabox']->id);
			
			// rotate ad flag
			$this->db->query('update bricaboxes set use_internal_ads = not use_internal_ads where id = ?', array($BBX['bricabox']->id));	
			//$this->output->enable_profiler(true);
			$this->load->view('bricabox/show', $data);
		}
		elseif (isset($BBX['app_domain']) && preg_match("/{$BBX['request']['subless_host']}/", $BBX['app_domain']->domain))
		{
			// redirect to global profile if logged in
			//if ($this->userland->logged_in())
			//{
			//	header('Location: /person/'.$this->userland->current_user()->login);
			//	exit();
			//}
			
			$cachefile = APPPATH.'../cache/homepage.html';
			if (false && file_exists($cachefile))
			{
				log_message('debug', 'using cached homepage');
				require_once $cachefile;
			}
			else
			{
				ob_start();
				
				// unified homepage
				$tpl = 'home_v2';
			
				$data['BBX'] 			= $BBX;
				$data['logged_in'] 		= $this->userland->logged_in();
			
				$_html = $this->load->view("pages/{$tpl}", $data, true);
				echo $_html;
				$html = ob_get_contents();
				
				// write the cache
				//$fp = fopen($cachefile, 'w');
				//fwrite($fp, $html);
				//fclose($fp);
				
				ob_end_clean();
				
				echo $html;
			}
		}
	}
	
	public function new_home()
	{
		$data = false;
		$this->load->view('pages/home_v2', $data);
	}
}
?>